package com.bwie.spike.conf;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.bwie.spike.dao.OrderInfoDao;
import com.bwie.spike.entity.OrderInfoEntity;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName MyRunner
 * @Description TODO
 * @Author 李奉武
 * @Motto 逆水行舟, 不进则退
 * @Date 2022/3/8 8:59
 * @Version 1.0
 */
@Component
public class MyRunner implements ApplicationRunner {

    @Autowired
    OrderInfoDao orderInfoDao;

    @Autowired
    StringRedisTemplate redisTemplate;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        List<OrderInfoEntity> orderInfoEntities = orderInfoDao.selectList(null);
        List<Object> order = redisTemplate.opsForHash().values("order");
        if (StringUtils.isNotBlank(order.toString())){
            redisTemplate.delete("order");
        }
        orderInfoEntities.stream().forEach((e)->{
            redisTemplate.opsForHash().put("order", e.getId().toString(), JSON.toJSONString(e));
        });
    }
}
